1/* Copyright 2016 The TensorFlow Authors. All Rights Reserved.
2
3Licensed under the Apache License, Version 2.0 (the "License");
4you may not use this file except in compliance with the License.
5You may obtain a copy of the License at
6
7 http://www.apache.org/licenses/LICENSE-2.0
8
9Unless required by applicable law or agreed to in writing, software
10distributed under the License is distributed on an "AS IS" BASIS,
11WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12See the License for the specific language governing permissions and
13limitations under the License.
14==============================================================================*/
15
16// See docs in ../ops/string_ops.cc.
17
18#include <string>
19
20#include "tensorflow/core/framework/op_kernel.h"
21#include "tensorflow/core/framework/tensor.h"
22#include "tensorflow/core/lib/strings/base64.h"
23
24namespace tensorflow {
25namespace {
26
27class EncodeBase64Op : public OpKernel {
28 public:
29 explicit EncodeBase64Op(OpKernelConstruction* context) : OpKernel(context) {
30 OP_REQUIRES_OK(context, context->GetAttr("pad", &pad_));
31 }
32
33 void Compute(OpKernelContext* context) override {
34 const Tensor& input_tensor = context->input(0);
35 Tensor* output_tensor = nullptr;
36 OP_REQUIRES_OK(context, context->allocate_output(0, input_tensor.shape(),
37 &output_tensor));
38
39 auto input = input_tensor.flat<tstring>();
40 auto output = output_tensor->flat<tstring>();
41
42 for (int64_t i = 0; i < input.dimension(0); ++i) {
43 OP_REQUIRES_OK(context, Base64Encode(input(i), pad_, &output(i)));
44 }
45 }
46
47 private:
48 bool pad_;
49};
50
51REGISTER_KERNEL_BUILDER(Name("EncodeBase64").Device(DEVICE_CPU),
52 EncodeBase64Op);
53
54class DecodeBase64Op : public OpKernel {
55 public:
56 using OpKernel::OpKernel;
57
58 void Compute(OpKernelContext* context) override {
59 const Tensor& input_tensor = context->input(0);
60 Tensor* output_tensor = nullptr;
61 OP_REQUIRES_OK(context, context->allocate_output(0, input_tensor.shape(),
62 &output_tensor));
63
64 auto input = input_tensor.flat<tstring>();
65 auto output = output_tensor->flat<tstring>();
66
67 for (int64_t i = 0; i < input.dimension(0); ++i) {
68 OP_REQUIRES_OK(context, Base64Decode(input(i), &output(i)));
69 }
70 }
71};
72
73REGISTER_KERNEL_BUILDER(Name("DecodeBase64").Device(DEVICE_CPU),
74 DecodeBase64Op);
75
76} // namespace
77} // namespace tensorflow
78